package com.axend.lib_base.view.dialog

import android.app.Dialog
import android.content.Context
import android.view.View.OnClickListener
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import com.axend.lib_base.R
import com.axend.lib_base.databinding.BaseDialogCustomCalibrationBinding
import com.axend.lib_base.ext.dp2px
import com.axend.lib_base.ext.roundToNearestInteger
import com.axend.lib_base.ext.toFloatOrDefault
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.util.concurrent.Flow

/**
 * @description效验dialog
 * @author admins
 * @time 2025/3/5 17:05
 */
class CustomCalibrationDialog(context: Context, onSuccessClickListener: OnClickListener,onCancelClickListener: OnClickListener) :
    Dialog(context, R.style.Common_CustomDialog) {

    private var dialogBinding: BaseDialogCustomCalibrationBinding
    private lateinit var timeJob: Job
    private val maxProgress = 50
    private val onCancelClickListener: OnClickListener = onCancelClickListener
    private val onSuccessClickListener: OnClickListener = onSuccessClickListener

    companion object {
        const val MODEL_LOADING = 0
        const val MODEL_FAILED = 1
        const val MODEL_SUCCESS = 2
        const val MODEL_DISMISS = 3
    }


    init {
        dialogBinding = DataBindingUtil.inflate(
            layoutInflater,
            R.layout.base_dialog_custom_calibration,
            null,
            false
        )
        setContentView(dialogBinding.root)
        setCanceledOnTouchOutside(false)
        val window = window
        window!!.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
        //设置提示框位置
        val lp = window!!.attributes
        //lp.gravity = Gravity.TOP | Gravity.LEFT;
        //lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
        //lp.x = DisplayUtils.dip2px(53);
        //lp.y = DisplayUtils.dip2px(186);
        lp.width = dp2px(300f)
        window.attributes = lp

        dialogBinding.btSure.setOnClickListener {
            dismiss()
        }


    }


    private fun createNumberFlow(maxProgress: Int) = flow {
        for (i in 1..maxProgress) {
            emit(i)
            delay(200)
        }
    }

    /**
     * 设置模式 model 0 加载 1 失败  2 成功
     */
    fun setModel(model: Int): CustomCalibrationDialog {
        when (model) {
            MODEL_LOADING -> {
                show()
                dialogBinding.progressBar.visibility = android.view.View.VISIBLE
                dialogBinding.llProgress.visibility = android.view.View.VISIBLE
                dialogBinding.llFailed.visibility = android.view.View.GONE
                dialogBinding.llSuccess.visibility = android.view.View.GONE
                dialogBinding.btSure.text = context.getString(R.string.app_dialog_cancel)
//                dialogBinding.ctProgressBar.setMaxProgress(maxProgress.toFloatOrDefault(0f))
//                dialogBinding.ctProgressBar.setProgress(0f)
//                timeJob = CoroutineScope(Dispatchers.Main).launch {
//                    createNumberFlow(maxProgress).collect {
//                        dialogBinding.ctProgressBar.setProgress(it.toFloatOrDefault(0f))
//                        var percentage : Float= ((it.toFloatOrDefault(0f) / maxProgress) * 100).toFloat()
//                        dialogBinding.ctProgressBar.setText("${percentage.roundToNearestInteger()}%")
//                    }
//                }
                dialogBinding.btSure.setOnClickListener { onCancelClickListener.onClick(it) }
            }

            MODEL_FAILED -> {
                show()
                dialogBinding.progressBar.visibility = android.view.View.GONE
                dialogBinding.llProgress.visibility = android.view.View.GONE
                dialogBinding.llFailed.visibility = android.view.View.VISIBLE
                dialogBinding.llSuccess.visibility = android.view.View.GONE
                dialogBinding.btSure.text = context.getString(R.string.app_dialog_ok)
                dialogBinding.btSure.setOnClickListener { dismiss() }
            }

            MODEL_SUCCESS -> {
                show()
                dialogBinding.progressBar.visibility = android.view.View.GONE
                dialogBinding.llProgress.visibility = android.view.View.GONE
                dialogBinding.llFailed.visibility = android.view.View.GONE
                dialogBinding.llSuccess.visibility = android.view.View.VISIBLE
                dialogBinding.btSure.text = context.getString(R.string.app_dialog_ok)
                dialogBinding.btSure.setOnClickListener { onSuccessClickListener.onClick(it) }
            }

            else -> {
                dismiss()
            }
        }
        return this
    }


    override fun dismiss() {
        super.dismiss()
//        timeJob?.cancel()
    }


}